package com.finance.rule.computer;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Created by CBWL on 2017/5/3.
 */
@Component
public class MajorAnalyzerFactory implements ApplicationContextAware,IMajorAnalyzerFactory{
    private  AutowireCapableBeanFactory capableBeanFactory;

    @Resource(name= "defaultMajorAnalyzer")
    private IMajorAnalyzer majorAnalyzer;

    @Override
    public IMajorAnalyzer getMajorAnalyzer(String className, String initJson) throws ClassNotFoundException {
        Class<IMajorAnalyzer> clasz = (Class<IMajorAnalyzer>) Class.forName(className);
        IMajorAnalyzer majorAnalyzer = this.capableBeanFactory.createBean(clasz);
        majorAnalyzer.init(initJson);
        return majorAnalyzer;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.capableBeanFactory = applicationContext.getAutowireCapableBeanFactory();
    }

    @Override
    public IMajorAnalyzer getDefaultMajorAnalyzer(){
        return this.majorAnalyzer;
    }
}
